<?php

namespace app\store\controller\apps\dealer;

use app\store\controller\Controller;
use app\common\service\qrcode\Poster;
use app\store\model\dealer\User as UserModel;
use app\store\model\dealer\Referee as RefereeModel;
use app\store\model\dealer\Setting as SettingModel;

/**
 * 分销商管理
 * Class User
 * @package app\store\controller\apps\dealer
 */
class User extends Controller
{
    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 分销商用户列表
     * @param string $search
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($search = '')
    {
        $model = new UserModel;
        return $this->fetch('index', [
            'list' => $model->getList($search),
            'basicSetting' => SettingModel::getItem('basic'),
        ]);
    }

    /**
     * 分销商用户列表
     * @param string $user_id
     * @param int $level
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function fans($user_id, $level = -1)
    {

        $model = new RefereeModel;
        return $this->fetch('fans', [
            'list' => $model->getList($user_id, $level),
            'basicSetting' => SettingModel::getItem('basic'),
        ]);
    }

    /**
     * 编辑分销商
     * @param $dealer_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit($dealer_id)
    {
        $model = UserModel::detail($dealer_id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        if ($model->edit($this->postData('model'))) {
            $RefereeModel = new RefereeModel();
            $referee_id = $this->postData('model')['referee_id'];
            $referee = $RefereeModel->where(["user_id"=>$dealer_id])->find();
            if(!empty($referee)){
                if($referee['dealer_id']!==$referee_id){
                    $RefereeModel->save(['dealer_id' => $referee_id],['id' => $referee['id']]);
                    $this->resetRefereeNum($referee['dealer_id']);
                    $this->resetRefereeNum($referee_id);
                }
            }else{
                $saveData = [
                    'user_id' => $dealer_id,
                    'dealer_id' => $referee_id,
                    'wxapp_id' => '10012',
                    'create_time' => time()
                ];
                $RefereeModel->allowField(true)->save($saveData);
                $this->resetRefereeNum($referee_id);
            }
            return $this->renderSuccess('更新成功', url('apps.dealer.user/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    //重置某经销商的团队人数
    public function resetRefereeNum($id){
        $RefereeModel = new RefereeModel();
        $refereeNum = $RefereeModel->where(["dealer_id"=>$id])->count();
        $UserModel = new UserModel();
        $UserModel->save(['first_num'=>$refereeNum],["user_id"=>$id]);
    }


    /**
     * 用户充值
     * @param $user_id
     * @param int $source 充值类型
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function recharge($user_id)
    {
        // 用户详情
        $model = UserModel::detail($user_id);
        if ($model->recharge($user_id, $this->postData('recharge')['balance'])) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }



    /**
     * 删除分销商
     * @param $dealer_id
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function delete($dealer_id)
    {
        $model = UserModel::detail($dealer_id);
        if (!$model->setDelete()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 分销商二维码
     * @param $dealer_id
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function qrcode($dealer_id)
    {
        $model = UserModel::detail($dealer_id);
        $Qrcode = new Poster($model);
        $this->redirect($Qrcode->getImage());
    }

}